package com.itheima.file;

import java.io.File;
import java.util.HashMap;

public class FileDemo4 {
    public static void main(String[] args) {
        File file = new File("E:\\IdeaProjects\\JavaSE_Code");
        HashMap<String , Integer> hm = new HashMap<>();
        getCount(hm,file);
        System.out.println(hm);

    }

    private static void getCount(HashMap<String, Integer> hm, File file) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            //是文件
            if (file1.isFile()) {
                String file1Name = file1.getName();//获取文件名
                String[] fileNameArr = file1Name.split("\\.");
                String fileEndName = "空类型";
                if (fileNameArr.length > 1){
                    fileEndName = fileNameArr[fileNameArr.length-1];
                    //System.out.println("fileEndName = " + fileEndName);
                }else {
                    fileEndName = "空类型";
                    //System.out.println("fileEndName = " + fileEndName);
                }
                //System.out.println("fileEndName = " + fileEndName);
                if (hm.containsKey(fileEndName)) {
                    Integer count = hm.get(fileEndName);
                    //System.out.println("hm.isEmpty() = " + hm.isEmpty());
                    //System.out.println("count = " + count);
                    hm.put(fileEndName,++count);
                }else {
                    hm.put(fileEndName,1);
                }
            }
            //是文件夹
            else {
                //System.out.println(" look hear ");
                //boolean b = hm.containsKey("文件夹");
                //System.out.println("b = " + b);
                if (hm.containsKey("文件夹")) {
                    //System.out.println("go hear");
                    Integer count = hm.get("文件夹");
                    //System.out.println("count = " + count);
                    hm.put("文件夹",++count);
                }else {
                    hm.put("文件夹",1);
                }
                //System.out.println("hm.isEmpty() = " + hm.isEmpty());
                getCount(hm,file1);
            }
        }

    }
}
